<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 14:35
 */

namespace HiCommon\Service;


use HiCommon\Repository\OrderCheckpointRepository;
use HiCommon\Repository\ShopCheckpointAppriseRepository;
use HiCommon\Repository\ShopRepository;
use HiCommon\Repository\UsersAppriseRepository;
use Modules\SmallApi\Service\UserService;
use HiCommon\Service\PublicService;

class ShopCheckpointAppriseService extends BaseService
{

    /**
     * 商家-车友评价列表
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_apprises_list_by_shopid($n_shop_id)
    {
        if($n_shop_id <= 0){
            return hello_error('门店id不能为空');
        }
        $data =  UsersAppriseRepository::checkpoint_apprises_by_id($n_shop_id)->toarray();
        foreach ($data['data'] as &$value)
        {
            $arr_user = $value['user'];
            $arr_user['head_img'] = $arr_user['head_img']['full_src'];
            $value = [
              'id'=>$value['id'],
              'shop_id'=>$value['shop_id'],
              'score'=>$value['score'],
              'content'=>$value['content'],
              'user'=>$arr_user,
              'created_at'=>$value['created_at'],
              'created_date'=>substr($value['created_at'],0,10),
            ];
        }
        $data = list_page_data_for_api($data);
        return  hello_success('成功',$data);
    }

    /**
     * 用户给店铺评分-年检预约单专属
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_shop_arrpise_submit($n_uid,$arr_input)
    {
        $n_order_id = intval($arr_input['order_id']);
        $n_score = intval($arr_input['score']);
        $s_content = string_trim($arr_input['content']);
        if($n_order_id <= 0){
            return hello_error('订单id不能为空');
        }
        if($n_score < 0 || $n_score > 5){
            return hello_error('评分错误');
        }
//        if(strlen($s_content) <= 0){
//            return hello_error('请填写评论内容');
//        }

        //查询商家id
        $res  = OrderCheckpointRepository::get_one($n_order_id,['field'=>['id','shop_id','appraise_state','complete_state','user_id']])->toarray();
        $n_shop_id = (int)$res['shop_id'];
        if(!$n_shop_id){
            return hello_error('商家不存在');
        }
        if($res['complete_state'] != 2){
            return hello_error('该订单暂时不能评价');
        }
        if($res['appraise_state'] == 2){
            return hello_error('订单已评价');
        }
        if($res['user_id'] != $n_uid){
            return hello_error('只能评价自己的订单');
        }

        //评价订单
        $res = OrderCheckpointService::api_auto_apprise_appointment_order($n_order_id,$n_shop_id,$n_uid,$n_score,$s_content);
        if($res['state'] != 0){
            return $res;
        }

        //增加用户积分
        $arr_order = OrderCheckpointRepository::get_one($n_order_id,['field'=>['id','price','refund_price']]);
        $n_total_fee = $arr_order['price'] - $arr_order['refund_price'];
        $arr_data = [
          'total_fee'=>$n_total_fee,
          'type'=>1,
          'order_id'=>$n_order_id,
          'order_type'=>2,
          'user_id'=>$n_uid,
          'remarks'=>'年检预约订单评价增加用户积分',
        ];
        $res = PublicService::user_integral_change($arr_data);
        return $res;
    }


    public function api_shop_arrpise_submit_small( $n_uid,$arr_input ){

        $temp['user_id'] = $n_uid;
        $temp['shop_id'] = empty( $arr_input['shop_id'] ) ? 0 : $arr_input['shop_id'];
        $temp['img'] = empty( $arr_input['img'] ) ? '' :$arr_input['img'];
        $temp['content'] = empty( $arr_input['content'] ) ? '' : $arr_input['content'];
        $temp['score'] = empty( $arr_input['score'] ) ? 0 : $arr_input['score'];
        $temp['order_id'] = empty( $arr_input['order_id'] ) ? 0 : $arr_input['order_id'];
        $temp['goods_id'] = empty( $arr_input['goods_id'] ) ? 0 : $arr_input['goods_id'];
        $temp['type'] = 11;

        return \HiCommon\Service\MessageServce::apprise_create( (array) $temp );
    }





}